#include <stdio.h>
#include <assert.h>
#include <stdlib.h>  		/* for mkstemp(), */
#include <string.h>		/* for strcmp(),  */
#include <unistd.h> 				/* for unlink() */
#include <sys/wait.h>				/* for waitpid() */
#include "globals.h" 		/* for the tree node  */
#include "dot.h"
#include "tree.h"
#include "class2pic.h"



FILE	*listing;
char *file_name;

extern tree total_tree;

static int usage();		
static int check_type(char *);
int yyparse();
int yywrap();

char ** file_list;

int
main(int argc, char* argv[])
{
	FILE *file;
	int retval;
	char *f_list[20], *otype = 0; 
	file_list = f_list;
	char buf[BUFSIZE/8];
	int i = 0,n;
	int only_parser_flag = 0;

#ifdef DEBUG
	listing = stdout;	/* this listing is someting like log */
#else
	listing = fopen("/dev/null","w");
#endif

	if (argc < 2 )
		return usage();

	for (n = 1; n < argc; n++) {
		if (*argv[n] == '-') {
			switch(*(argv[n]+1)) {
			case 'T': /* the final type you want  */
				otype = argv[n]+2;
				//DEBUGOUT1(otype);
				if (check_type(otype)) {
					exit(1);
				}
				goto end_of_for; /* process next argument */
				break;
			case 'p':
				only_parser_flag = 1;
				break;
			case 'v':
				printf("class2pic 0.01\nWritten by Kzjeef\n");
				exit(0);
				break;
			case '?':
			case 'h':
			default:
				usage();
				exit(1);
				break;
			}
		}
		file_list[i++] = argv[n];
	end_of_for: ;
	}
		
	for (n  = 0; n < i; n++) {
		
		file_name = file_list[n];
		file = fopen(file_name, "r");
		if (!file) {
			fprintf(stderr,"file error, cann\'t open %s\n",file_name);
			return 1;
		}
		yyin = file;
		/* if one file parse failed , report and heading to next;*/
		retval = yyparse();
		if (retval) {
			fprintf(stderr,"file: %s parse failed, no object file will be make\n", file_name);
			continue;
		}
/* 		if (error_num) { */
/* 			fprintf(stderr,"file: %s have error so , no image file will be found\n",file_name); */
/* 			continue; */
/* 		} */
		if (only_parser_flag)
			exit(0);
		
		if (!total_tree) {
			fprintf(stderr,"file: %s cann\'t make a syntax tree, no object file will be make\n", file_name);
			continue;
		}
#ifdef DEBUG
		tree_print(total_tree);
#endif
		
		// char *tmp_file_name = "/tmp/__TEMP_FOR_CLASS.dot";
		char *tmp_file_name = tempnam("/tmp","class2pic");
		/* TODO: put the tmp_file to /tmp dir is better */
		unlink(tmp_file_name);
		FILE *tmp = fopen(tmp_file_name,"w+");
		/* TODO: we use a defined temp file name because of in
		 * next step,we must use the name to complie the dot
		 * file, if you know some way to correct , thanks very
		 * much. when not define any of file, `dot` read from
		 * standard stream --this may help  */
				
		tree2dot(0, file_name, total_tree, tmp);
		/* fork a process and excute the command */
		char type_buf[10];
		snprintf(type_buf, sizeof(type_buf), "-T%s",otype);
		snprintf(buf, sizeof(buf), "%s.%s",file_name,otype);
		
		pid_t pid = fork();
		if (pid > 0){	/* child */
			if (execlp("dot","dot",type_buf,tmp_file_name,"-o",\
				   buf,(char*)0)<0) {
				exit(1);
			}
		} else {
			if (waitpid(pid,0,0)< 0)
				return 1;
			//unlink(tmp_file_name);
		}
	}
	return 0;
}


int usage() {
	printf("class2pic [-T{type}] [-?] [-h] [-C{dirs}][-vp] [source files]\n");
	return 1;
}

int check_type(char *type) {
	assert(type);
	static char *type_list[] = {
		"ps",
		"svg",
		"fig",
		"mif",
		"hpgl",
		"pcl",
		"png",
		"gif",
		"imap",
		"cmapx",
		"dot"
	};
	char **t;
	for (t = type_list; t != 0; t++) {
		if (!strcmp(type, *t))
			return 0;
	}

	fprintf(stderr, "Error: we do not support this format:%s\n only support:\n",type);
	for (t = type_list; t != 0; t++) {
		fprintf(stderr,"%s%s ",*t,(t+1)? "," : ".\n");
	}
	return 1;
}
			



